package com.lc.mode;

import com.lc.enums.RequestType;
import com.sun.jndi.cosnaming.CNCtx;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * @author liuc
 * @date 2021/5/5
 * @description 请求
 **/
public class Request {
    private InputStream inputStream;
    private RequestType requestType;
    private String path;
    private String hostName;
    private Map<String, String> parameters;
    private byte[] content;

    public Request(){

    }

    public Request(byte[] bytes){
        this.content = bytes;
        if(content != null){
            inputStream = new ByteArrayInputStream(content);
            String reqStr = new String(content);
            reqStr = reqStr.trim();
            String[] headerArr = reqStr.split("\r\n");
            //解析第一行
            String firstHeader = headerArr[0];
            String[] reqArr = firstHeader.split(" ");
            this.requestType = RequestType.match(reqArr[0]);
            this.path = reqArr[1];
            for (int i = 1; i < headerArr.length; ++i){
                if(headerArr[i].startsWith("Host")){
                    //需要记录Host
                    String[] arr = headerArr[i].split(":");
                    //去端口号
                    this.hostName = arr[1].split(":")[0].trim();
                }
            }
            System.out.println(firstHeader + " " + this.hostName);
        }
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public RequestType getRequestType() {
        return requestType;
    }

    public void setRequestType(RequestType requestType) {
        this.requestType = requestType;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Map<String, String> getParameters() {
        return parameters;
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
}
